package com.aplink.generic.xmlparser;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.type.EnableConditionType;
import com.aplink.generic.constant.type.TargetRuleOperator;
import com.aplink.generic.util.objectrule.EnableCondition;
import com.aplink.generic.util.objectrule.Rule;

public class EnableConditionParser {
    public static final String COLUMN = "column";
    public static final String ENABLE_CONDITON = "enableCondition";
    public static final String INVALID = "invalid";
    public static final String OPERATOR = "operator";
    public static final String RULE = "rule";
    public static final String TYPE = "type";
    public static final String VALID = "valid";

    public static EnableCondition parse(final Node nodeFormat) {
        final EnableCondition targetCollection = new EnableCondition();
        final Element elementFormat = (Element) nodeFormat;
        final NodeList nodeListFormat = elementFormat
                .getElementsByTagName(EnableConditionParser.ENABLE_CONDITON);
        if (nodeListFormat.getLength() == 0) {
            return null;
        }
        final Node node = nodeListFormat.item(0);
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    EnableConditionParser.TYPE);
            if (!TextUtils.isEmpty(value)) {
                targetCollection.setType(value);
            }
            value = XMLPaserUtil
                    .getAttrValue(node, EnableConditionParser.VALID);
            if (!TextUtils.isEmpty(value)) {
                targetCollection.setValidType(EnableConditionType.get(value));
            }
            value = XMLPaserUtil.getAttrValue(node,
                    EnableConditionParser.INVALID);
            if (!TextUtils.isEmpty(value)) {
                targetCollection.setInvalidType(EnableConditionType.get(value));
            }
        }
        targetCollection.setRules(EnableConditionParser.parseRules(node));
        return targetCollection;
    }

    public static ArrayList<Rule> parseRules(final Node node) {
        final ArrayList<Rule> rules = new ArrayList<Rule>();
        final NodeList ruleList = ((Element) node)
                .getElementsByTagName(EnableConditionParser.RULE);
        Node ruleNode = null;
        for (int i = 0; i < ruleList.getLength(); i++) {
            ruleNode = ruleList.item(i);
            final Rule rule = new Rule();
            if (ruleNode.hasAttributes()) {
                String value = XMLPaserUtil.getAttrValue(ruleNode,
                        EnableConditionParser.OPERATOR);
                if (!TextUtils.isEmpty(value)) {
                    rule.setOperator(TargetRuleOperator.get(value));
                }

                value = XMLPaserUtil.getAttrValue(ruleNode,
                        EnableConditionParser.COLUMN);
                if (!TextUtils.isEmpty(value)) {
                    rule.setColumn(value);
                }

                value = XMLPaserUtil.getNodeValue(ruleNode);
                if (!TextUtils.isEmpty(value)) {
                    rule.setValue(value);
                }
            }
            rules.add(rule);
        }
        return rules;
    }
}
